<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="路线的id" prop="routeId">
      <el-input v-model="dataForm.routeId" placeholder="路线的id"></el-input>
    </el-form-item>
    <el-form-item label="阶段名称" prop="actTitle">
      <el-input v-model="dataForm.actTitle" placeholder="阶段名称"></el-input>
    </el-form-item>
    <el-form-item label="阶段课程id（1，2，3）" prop="actCourse">
      <el-input v-model="dataForm.actCourse" placeholder="阶段课程id（1，2，3）"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="状态  0：禁用   1：正常" prop="isStatus">
      <el-input v-model="dataForm.isStatus" placeholder="状态  0：禁用   1：正常"></el-input>
    </el-form-item>
    <el-form-item label="注册时间" prop="gmtCreate">
      <el-input v-model="dataForm.gmtCreate" placeholder="注册时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="gmtModified">
      <el-input v-model="dataForm.gmtModified" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          routeId: '',
          actTitle: '',
          actCourse: '',
          sort: '',
          isStatus: '',
          gmtCreate: '',
          gmtModified: ''
        },
        dataRule: {
          routeId: [
            { required: true, message: '路线的id不能为空', trigger: 'blur' }
          ],
          actTitle: [
            { required: true, message: '阶段名称不能为空', trigger: 'blur' }
          ],
          actCourse: [
            { required: true, message: '阶段课程id（1，2，3）不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          isStatus: [
            { required: true, message: '状态  0：禁用   1：正常不能为空', trigger: 'blur' }
          ],
          gmtCreate: [
            { required: true, message: '注册时间不能为空', trigger: 'blur' }
          ],
          gmtModified: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/ly/studyrouteactivitiy/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.routeId = data.studyRouteActivitiy.routeId
                this.dataForm.actTitle = data.studyRouteActivitiy.actTitle
                this.dataForm.actCourse = data.studyRouteActivitiy.actCourse
                this.dataForm.sort = data.studyRouteActivitiy.sort
                this.dataForm.isStatus = data.studyRouteActivitiy.isStatus
                this.dataForm.gmtCreate = data.studyRouteActivitiy.gmtCreate
                this.dataForm.gmtModified = data.studyRouteActivitiy.gmtModified
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/ly/studyrouteactivitiy/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'routeId': this.dataForm.routeId,
                'actTitle': this.dataForm.actTitle,
                'actCourse': this.dataForm.actCourse,
                'sort': this.dataForm.sort,
                'isStatus': this.dataForm.isStatus,
                'gmtCreate': this.dataForm.gmtCreate,
                'gmtModified': this.dataForm.gmtModified
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
